import { Component, OnInit } from '@angular/core';
import {SnackSuccessComponent} from '../../snack-success/snack-success.component';
import {Resume} from '../../../entity/resume';
import {SnackErrorComponent} from '../../snack-error/snack-error.component';
import {TalentService} from '../../../service/talent.service';
import {SnackService} from '../../../service/snack.service';
import {MatDialog, MatSnackBar} from '@angular/material';
import {DelDialogComponent} from '../del-dialog/del-dialog.component';

@Component({
  selector: 'app-resume-list',
  templateUrl: './resume-list.component.html',
  styleUrls: ['./resume-list.component.css', '../../position-details/position-details-dialog.component.css']
})
export class ResumeListComponent implements OnInit {

  resumes: any = new Array<Resume>();
  constructor(
    public talentService: TalentService,
    private snackService: SnackService,
    private snackBar: MatSnackBar,
    public dialog: MatDialog
  ) {
    this.loadData();
  }

  ngOnInit() {
  }
  loadData() {
    let seq = this.talentService.findPositonList();
    seq.subscribe(resumes => this.resumes = resumes);
  }
  delResume(id: number) {
    const seq = this.talentService.delResume(id);
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '删除成功';
        this.loadData();
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        // this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });
  }
  openDialog(id: number): void {
    let dialogRef = this.dialog.open(DelDialogComponent, {
      width: '250px',
    });

    dialogRef.afterClosed().subscribe(result => {
      if (result == 'next') {
        this.delResume(id);
      }
    });
  }
}
